unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, DBClient, DBCtrls, Mask;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    EdtTotal: TEdit;
    EdtEntrada: TEdit;
    EdtParcelas: TEdit;
    EdtJuro: TEdit;
    Caclular: TButton;
    Cds: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    CdsParcela: TIntegerField;
    CdsSaldoDevido: TCurrencyField;
    CdsAmortizacao: TCurrencyField;
    CdsJuros: TCurrencyField;
    CdsPrestacao: TCurrencyField;
    EdtTR: TEdit;
    Label5: TLabel;
    LbTotPrestacao: TLabel;
    CdsTotPrestacao: TAggregateField;
    procedure FormCreate(Sender: TObject);
    procedure CaclularClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CaclularClick(Sender: TObject);
var i: integer;
    Amortizacao,Total, Entrada, VlDevido, Juro: currency;
    Parcelas: integer;
begin
  cds.EmptyDataSet;

  Total := StrToCurr(EdtTotal.Text);
  Entrada := StrToCurr(EdtEntrada.Text);
  Parcelas := StrToInt(EdtParcelas.Text);
  VlDevido := Total - Entrada;
  Juro := (StrToCurr(EdtJuro.Text) + StrToCurr(EdtTR.Text)) / 12;
  Amortizacao := (Total - Entrada) / Parcelas;

  Cds.Append;
  CdsParcela.AsInteger := 0;
  CdsSaldoDevido.AsCurrency := VlDevido;
  Cds.Post;

  for i := 1 to Parcelas do begin
    Cds.Append;

    CdsParcela.AsInteger := i;

    CdsJuros.AsCurrency := VlDevido * Juro / 100;
    VlDevido := VlDevido - Amortizacao;
    CdsSaldoDevido.AsCurrency := VlDevido;

    CdsAmortizacao.AsCurrency := Amortizacao;
    CdsPrestacao.AsCurrency := Amortizacao + CdsJuros.AsCurrency;
    Cds.Post;
  end;

  LbTotPrestacao.Caption := 'R$' + CdsTotPrestacao.AsString;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Cds.CreateDataSet;
end;

end.
